package com.woniuxy.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.common.Result;
import com.woniuxy.controller.form.AccountCheckForm;
import com.woniuxy.controller.form.AccountLoginForm;
import com.woniuxy.controller.form.AccountRegisterForm;
import com.woniuxy.controller.form.AccountUpdateForm;
import com.woniuxy.entity.Account;
import com.woniuxy.exception.parameter.ParameterValidateException;
import com.woniuxy.service.AccountService;
import com.woniuxy.service.dto.AccountDto;
import com.woniuxy.util.JwtUtil;
import com.woniuxy.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zz
 * @since 2023年06月13日
 */
@RestController
@RequestMapping("/account")
@Api(tags = "后台账号相关接口")
public class AccountController {
    @Autowired
    private AccountService accountService;

    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("/login")
    @ApiOperation("后台账号登录")
    public Result login(@RequestBody AccountLoginForm accountLoginForm, HttpServletResponse resp) throws Exception {
        Account account =accountService.login(accountLoginForm);
        HashMap<String, Object> body = new HashMap<>();
        body.put("sub",account.getId());
        Integer minites = 30;
        String accessToken = JwtUtil.createToken(body, minites);
        String refreshToken = JwtUtil.createToken(body,24*60);
        redisUtil.setString(accessToken,refreshToken,24*60*60); //redis保存双token
        resp.setHeader("accessToken",accessToken);
        AccountDto accountDto = BeanUtil.toBean(account, AccountDto.class);
        return Result.success(200,"登录成功",accountDto); //删除了密码等敏感信息
    }


    @PostMapping("/update")
    @ApiOperation("后台账号修改基本信息")
    public Result update(@RequestBody AccountUpdateForm form,@RequestHeader String accessToken) throws Exception {

        Integer accountId = redisUtil.getUserIdByToken(accessToken);
        accountService.updateAccount(form,Long.valueOf(accountId.toString()));
        return Result.success(200,"修改成功",null);
    }

    @GetMapping("/sendCode")
    @ApiOperation("发送登录验证码")
    @ApiImplicitParam(name = "phone",value = "电话")
    public Result sendLoginCode(String phone) throws Exception {
        accountService.sendCode(phone);
        return Result.success(200,"发送短信验证码成功",null);
    }

    @PostMapping("/check")
    @ApiOperation("注册时验证账号存在")
    public Result check(@RequestBody @Valid AccountCheckForm form,BindingResult br) throws Exception {

        if(br.hasErrors()){
            throw new ParameterValidateException(500,"参数异常");
        }
        String result =accountService.check(form);
        return Result.success(200,"验证账号成功",result);
    }

    @PostMapping("/register")
    @ApiOperation("申请基本账号")
    public Result register(@RequestBody AccountRegisterForm form) throws Exception {
        Account account=accountService.register(form);
        AccountDto accountDto = BeanUtil.toBean(account, AccountDto.class);
        return Result.success(200,"注册成功",accountDto);
    }



}

